package com.sunfeng.chap14;

import java.nio.ByteBuffer;

/**
 * 只要是调用get则会触发position增加
 */
public class Test5_6 {
    public static void main(String[] args) {
        byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
        //wrap  包裹  allocate  分配规划
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);

        byte [] readByte = new byte[5];

        while (byteBuffer.hasRemaining()) {
            int readLength = Math.min(byteBuffer.remaining(), readByte.length );
            //从缓冲区读数据
            byteBuffer.get(readByte, 0, readLength);
            for (int i = 0; i < readLength; i++) {
                System.out.print(readByte[i] + " ");
            }
            System.out.println();
        }

    }
}
